#include "bike_dbus.h"

#define BIKE_SERVICE "com.readygo.bike"
#define BIKE_SERVICE_PATH "/interface"
#define BIKE_INTERFACE  "com.readygo.bike"

BikeDbus::BikeDbus(QObject *parent):
    QObject(parent)
{
    QDBusConnection session  = QDBusConnection::sessionBus();
    if (session.isConnected()) {
        bikeIface = new QDBusInterface(BIKE_SERVICE, BIKE_SERVICE_PATH,
                         BIKE_INTERFACE, QDBusConnection::sessionBus());
        if (!bikeIface->isValid()) {
            qDebug() << "bikeIface invalid";
            bikeIface = NULL;
        }

        connect(this, SIGNAL(SensorDataChanged(QVariantMap data)), this, SLOT(onSensorDataChanged(QVariantMap data)));
    }
}

BikeDbus::~BikeDbus()
{
    delete bikeIface;
    bikeIface = NULL;
}

QString BikeDbus::getMapDataPath()
{
    QDBusReply<QString> reply = bikeIface.call("GetMapDataPath");
    if (reply.isValid()) {
        qDebug() << "GetMapDataPath: " << reply.value();
    } else {
        qDebug() << "GetMapDataPath invalid";
    }

    return reply.value();
}

QVariantMap BikeDbus::getSensorData()
{
      QDBusReply<QVariantMap> reply = bikeIface.call("GetSensorData");
      if (reply.isValid()) {
          qDebug() << "GetSensorData: " << reply.value();
      } else {
          qDebug() << "GetSensorData failed";
      }

      return reply.value();
}

void BikeDbus::onSensorDataChanged(QVariantMap data)
{

}
